#!/usr/bin/ruby
# -*- ruby -*-
#
# http://www.reddit.com/r/dailyprogrammer/comments/1iambu/071513_challenge_133_easy_foottraffic_analysis/

require 'pp'
require 'set'

rooms = {}

Room = Struct.new(:visitors, :time_in_room)

STDIN.readlines.each do |l|
  l.strip!

  t = l.split(' ')

  next unless t.nil? || t.length == 4

  visitor = t[0].to_i
  room    = t[1].to_i
  op      = t[2]
  time    = t[3].to_i

  rooms[room] ||= Room.new
  rooms[room].visitors ||= {}
  rooms[room].time_in_room ||= 0

  if op == 'I'
    # Entering room
    rooms[room].visitors[visitor] = time
  elsif op == 'O'
    # Leaving room
    visitor_time_in_room = time - rooms[room].visitors[visitor]

    rooms[room].time_in_room += visitor_time_in_room
  end
end

rooms.keys.sort.each do |room|
  total_visitors = rooms[room].visitors.size
  total_time = rooms[room].time_in_room

  puts [room, total_time / total_visitors, total_visitors].join(' ')
end
